﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Script.Serialization;

namespace com.igetui.api.openservice.utils
{
	// Token: 0x02000037 RID: 55
	public static class ApnsUtils
	{
		// Token: 0x060004D2 RID: 1234 RVA: 0x0000B81C File Offset: 0x00009A1C
		public static int validatePayloadLength(string locKey, string locArgs, string message, string actionLocKey, string launchImage, string badge, string sound, string payload, int contentAvailable)
		{
			string text = ApnsUtils.processPayload(locKey, locArgs, message, actionLocKey, launchImage, badge, sound, payload, contentAvailable);
			int result;
			try
			{
				Console.WriteLine(text);
				result = Encoding.UTF8.GetBytes(text).Length;
			}
			catch (Exception innerException)
			{
				throw new Exception("payload json get bytese error", innerException);
			}
			return result;
		}

		// Token: 0x060004D3 RID: 1235 RVA: 0x0000B874 File Offset: 0x00009A74
		public static bool validatePayload(string locKey, string locArgs, string message, string actionLocKey, string launchImage, string badge, string sound, string payload, int contentAvailable)
		{
			return ApnsUtils.validatePayloadLength(locKey, locArgs, message, actionLocKey, launchImage, badge, sound, payload, contentAvailable) <= 256;
		}

		// Token: 0x060004D4 RID: 1236 RVA: 0x0000B8A0 File Offset: 0x00009AA0
		private static string processPayload(string locKey, string locArgs, string message, string actionLocKey, string launchImage, string badge, string sound, string payload, int contentAvailable)
		{
			bool flag = false;
			ApnsUtils.Payload payload2 = new ApnsUtils.Payload();
			if (locKey != null && locKey.Length > 0)
			{
				payload2.AlertLocKey = locKey;
				if (locArgs != null && locArgs.Length > 0)
				{
					payload2.AlertLocArgs = locArgs.Split(new char[]
					{
						','
					});
				}
				flag = true;
			}
			if (message != null && message.Length > 0)
			{
				payload2.AlertBody = message;
				flag = true;
			}
			if (actionLocKey != null && actionLocKey.Length > 0)
			{
				payload2.AlertActionLocKey = actionLocKey;
			}
			if (launchImage != null && launchImage.Length > 0)
			{
				payload2.AlertLaunchImage = launchImage;
			}
			int num = -1;
			try
			{
				num = int.Parse(badge);
			}
			catch (Exception innerException)
			{
				throw new Exception("unBindAlias失败 {0}", innerException);
			}
			if (num >= 0)
			{
				payload2.Badge = num;
				flag = true;
			}
			if (sound != null && sound.Length > 0)
			{
				payload2.Sound = sound;
			}
			else
			{
				payload2.Sound = "default";
			}
			if (payload != null && payload.Length > 0)
			{
				payload2.addParam("payload", payload);
			}
			if (contentAvailable == 1)
			{
				payload2.ContentAvailable = 1;
			}
			if (!flag)
			{
				throw new Exception("one of the params(locKey,message,badge) must not be null or contentAvailable must be 1");
			}
			string text = payload2.ToString();
			if (text == null)
			{
				throw new Exception("payload json is null");
			}
			return text;
		}

		// Token: 0x02000073 RID: 115
		public class Payload
		{
			// Token: 0x17000444 RID: 1092
			// (get) Token: 0x06000AD5 RID: 2773 RVA: 0x0001964B File Offset: 0x0001784B
			// (set) Token: 0x06000AD6 RID: 2774 RVA: 0x00019653 File Offset: 0x00017853
			public Dictionary<string, object> Paramss
			{
				get
				{
					return this.paramss;
				}
				set
				{
					this.paramss = value;
				}
			}

			// Token: 0x17000445 RID: 1093
			// (get) Token: 0x06000AD7 RID: 2775 RVA: 0x0001965C File Offset: 0x0001785C
			// (set) Token: 0x06000AD8 RID: 2776 RVA: 0x00019664 File Offset: 0x00017864
			public string Alert
			{
				get
				{
					return this.alert;
				}
				set
				{
					this.alert = value;
				}
			}

			// Token: 0x17000446 RID: 1094
			// (get) Token: 0x06000AD9 RID: 2777 RVA: 0x0001966D File Offset: 0x0001786D
			// (set) Token: 0x06000ADA RID: 2778 RVA: 0x00019675 File Offset: 0x00017875
			public int Badge
			{
				get
				{
					return this.badge;
				}
				set
				{
					this.badge = value;
				}
			}

			// Token: 0x17000447 RID: 1095
			// (get) Token: 0x06000ADB RID: 2779 RVA: 0x0001967E File Offset: 0x0001787E
			// (set) Token: 0x06000ADC RID: 2780 RVA: 0x00019686 File Offset: 0x00017886
			public string Sound
			{
				get
				{
					return this.sound;
				}
				set
				{
					this.sound = value;
				}
			}

			// Token: 0x17000448 RID: 1096
			// (get) Token: 0x06000ADD RID: 2781 RVA: 0x0001968F File Offset: 0x0001788F
			// (set) Token: 0x06000ADE RID: 2782 RVA: 0x00019697 File Offset: 0x00017897
			public string AlertBody
			{
				get
				{
					return this.alertBody;
				}
				set
				{
					this.alertBody = value;
				}
			}

			// Token: 0x17000449 RID: 1097
			// (get) Token: 0x06000ADF RID: 2783 RVA: 0x000196A0 File Offset: 0x000178A0
			// (set) Token: 0x06000AE0 RID: 2784 RVA: 0x000196A8 File Offset: 0x000178A8
			public string AlertActionLocKey
			{
				get
				{
					return this.alertActionLocKey;
				}
				set
				{
					this.alertActionLocKey = value;
				}
			}

			// Token: 0x1700044A RID: 1098
			// (get) Token: 0x06000AE1 RID: 2785 RVA: 0x000196B1 File Offset: 0x000178B1
			// (set) Token: 0x06000AE2 RID: 2786 RVA: 0x000196B9 File Offset: 0x000178B9
			public string AlertLocKey
			{
				get
				{
					return this.alertLocKey;
				}
				set
				{
					this.alertLocKey = value;
				}
			}

			// Token: 0x1700044B RID: 1099
			// (get) Token: 0x06000AE3 RID: 2787 RVA: 0x000196C2 File Offset: 0x000178C2
			// (set) Token: 0x06000AE4 RID: 2788 RVA: 0x000196CA File Offset: 0x000178CA
			public string[] AlertLocArgs
			{
				get
				{
					return this.alertLocArgs;
				}
				set
				{
					this.alertLocArgs = value;
				}
			}

			// Token: 0x1700044C RID: 1100
			// (get) Token: 0x06000AE5 RID: 2789 RVA: 0x000196D3 File Offset: 0x000178D3
			// (set) Token: 0x06000AE6 RID: 2790 RVA: 0x000196DB File Offset: 0x000178DB
			public string AlertLaunchImage
			{
				get
				{
					return this.alertLaunchImage;
				}
				set
				{
					this.alertLaunchImage = value;
				}
			}

			// Token: 0x1700044D RID: 1101
			// (get) Token: 0x06000AE7 RID: 2791 RVA: 0x000196E4 File Offset: 0x000178E4
			// (set) Token: 0x06000AE8 RID: 2792 RVA: 0x000196EC File Offset: 0x000178EC
			public int ContentAvailable
			{
				get
				{
					return this.contentAvailable;
				}
				set
				{
					this.contentAvailable = value;
				}
			}

			// Token: 0x06000AE9 RID: 2793 RVA: 0x000196F5 File Offset: 0x000178F5
			public void addParam(string key, object obj)
			{
				if (this.paramss == null)
				{
					this.paramss = new Dictionary<string, object>();
				}
				if (ApnsUtils.Payload.APS.Equals(key.ToLower()))
				{
					throw new Exception("the key can't be aps");
				}
				this.paramss.Add(key, obj);
			}

			// Token: 0x06000AEA RID: 2794 RVA: 0x00019734 File Offset: 0x00017934
			public override string ToString()
			{
				Dictionary<string, object> dictionary = new Dictionary<string, object>();
				Dictionary<string, object> dictionary2 = new Dictionary<string, object>();
				if (this.Alert != null)
				{
					dictionary2.Add("alert", this.Alert);
				}
				else if (this.AlertBody != null || this.AlertLocKey != null)
				{
					Dictionary<string, object> dictionary3 = new Dictionary<string, object>();
					this.putIntoJson("body", this.AlertBody, dictionary3);
					this.putIntoJson("action-loc-key", this.AlertActionLocKey, dictionary3);
					this.putIntoJson("loc-key", this.AlertLocKey, dictionary3);
					this.putIntoJson("launch-image", this.AlertLaunchImage, dictionary3);
					if (this.AlertLocArgs != null)
					{
						List<string> list = new List<string>();
						foreach (string item in this.AlertLocArgs)
						{
							list.Add(item);
						}
						dictionary3.Add("loc-args", list);
					}
					dictionary2.Add("alert", dictionary3);
				}
				dictionary2.Add("badge", this.Badge);
				if (!"com.gexin.ios.silence".Equals(this.Sound))
				{
					this.putIntoJson("sound", this.Sound, dictionary2);
				}
				if (this.contentAvailable == 1)
				{
					dictionary2.Add("content-available", this.ContentAvailable);
				}
				dictionary.Add(ApnsUtils.Payload.APS, dictionary2);
				if (this.Paramss != null)
				{
					foreach (KeyValuePair<string, object> keyValuePair in this.Paramss)
					{
						dictionary.Add(keyValuePair.Key, keyValuePair.Value);
					}
				}
				string result;
				try
				{
					result = new JavaScriptSerializer().Serialize(dictionary);
				}
				catch (Exception innerException)
				{
					throw new Exception("build apn payload json error", innerException);
				}
				return result;
			}

			// Token: 0x06000AEB RID: 2795 RVA: 0x0001990C File Offset: 0x00017B0C
			private void putIntoJson(string key, string value, Dictionary<string, object> obj)
			{
				if (value != null)
				{
					obj.Add(key, value);
				}
			}

			// Token: 0x04000375 RID: 885
			private static string APS = "aps";

			// Token: 0x04000376 RID: 886
			private Dictionary<string, object> paramss;

			// Token: 0x04000377 RID: 887
			private string alert;

			// Token: 0x04000378 RID: 888
			private int badge;

			// Token: 0x04000379 RID: 889
			private string sound = "";

			// Token: 0x0400037A RID: 890
			private string alertBody;

			// Token: 0x0400037B RID: 891
			private string alertActionLocKey;

			// Token: 0x0400037C RID: 892
			private string alertLocKey;

			// Token: 0x0400037D RID: 893
			private string[] alertLocArgs;

			// Token: 0x0400037E RID: 894
			private string alertLaunchImage;

			// Token: 0x0400037F RID: 895
			private int contentAvailable;
		}
	}
}
